from flask import Blueprint, request, jsonify, redirect
import json
from dbs import dbs

bp_login = Blueprint('login', __name__)


@bp_login.route('/login', methods=['POST', 'GET'])
def login():
    if request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        sql = 'select * from lusers where username = "%s"' % (data['username'])
        res = dbs.findone(sql)
        if res:
            if res['password'] == data['password']:
                return jsonify({'code': 200, 'userid': res['id'], 'username': res['username']})
            else:
                return jsonify({'code': 20011, 'mes': '密码错误'})
        else:
            return jsonify({'code': 20012, 'mes': '用户不存在'})


@bp_login.route('/sfM', methods=['POST', 'GET'])
def sfM():
    if request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        token = data['token']
        uid = data['uid']
        sql = """select * from sf_login where token = '%s' and uid = '%s'""" % (token, uid)
        res = dbs.findone(sql)
        if res:

            return jsonify({'code':200,'url':'http://localhost:8080/#/','userid':res['userid']})
        else:
            return jsonify({"url": 'http://localhost:8080/#/sanfang'})
            # return jsonify('http://localhost:8080/#/sanfang')


@bp_login.route('/dengluM', methods=['POST', 'GET'])
def dengluM():
    if request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        token = data['token']
        mobile = data['username']
        uid = int(data['uid'])
        sql9 = 'select id from lusers where username = "%s"' % (mobile)
        res9 = dbs.findone(sql9)
        if not res9:
            sql = """insert into lusers(username) values ('%s')""" % (mobile)
            dbs.update(sql)
        sql1 = """select * from lusers where username= ('%s')""" % (mobile)
        res = dbs.findone(sql1)
        sql2 = """insert into sf_login(userid,uid,token) values (%d,%d,'%s')""" % (int(res['id']), uid, token)
        dbs.update(sql2)
        return jsonify({'code': 200, "mes": "登录成功", "url": 'http://localhost:8080', 'userid': res['id']})


@bp_login.route('/uidtoken')
def uidtoken():
    uid = '123456789'
    token = 'qwert789'
    return jsonify({'code':200, 'uid': uid, 'ltoken': token})

import datetime
import random
@bp_login.route('/shop',methods=['POST','GET'])
def shop():
    if request.method == 'GET':
        sql = 'select * from shop'
        res = dbs.findall(sql)
        return jsonify({'code':200,'list':res})
    elif request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        order_no = datetime.datetime.strftime(datetime.datetime.now(),"%Y%m%d%H%M%S")+str(data['userid'])+str(random.randint(1000,9999))
        sql1 = 'select price from shop where id = %d'%(int(data['id']))
        res1 = dbs.findone(sql1)
        sql2 = 'insert into order(userid,shopid,order_no,money,status) values(%d,%d,"%s",%d,1)'%(int(data['userid']),int(data['id']),order_no,int(res1['price']))
        dbs.update(sql2)
        return jsonify({'code':200,'order_no':order_no})